﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Blue.ResourceManager;
using System.IO;

namespace EngineOfEvermore.Editor.Common.Controls
{
    public partial class SaveResourceDialog : Form
    {
        String _selectedResourceName;

        public SaveResourceDialog()
        {
            InitializeComponent();

            if ( !this.DesignMode )
            {
                throw new Exception( "Use other constructor" );
            }
        }

        public SaveResourceDialog( String rootDirectory, String extensions )
        {
            InitializeComponent();

            _resourceExplorer.RootDirectory = rootDirectory;
            _resourceExplorer.ResourceExtensions = extensions;
        }

        public String SelectedResourceName
        {
            get
            {
                return _selectedResourceName;
            }
        }

        void _shown( object sender, EventArgs e )
        {
            _updateButtonsAndSelectionLabel();
        }

        void _resourceExplorer_SelectionChanged( object sender, ResourceNameEventArgs e )
        {
            _updateButtonsAndSelectionLabel();
        }

        void _updateButtonsAndSelectionLabel()
        {
            if ( this.DesignMode )
            {
                return;
            }

            _selectionTextBox.Text = _resourceExplorer.SelectedResourceName;
        }

        void _resourceExplorer_ResourceOpened( object sender, ResourceNameEventArgs e )
        {
            _tryCloseWithOk();
        }

        void _tryCloseWithOk()
        {
            _selectedResourceName = "";

            String proposedResourceName = ResourceManager.NormalizeName( _selectionTextBox.Text );

            if ( String.IsNullOrEmpty( proposedResourceName ) )
            {
                MessageBox.Show( "Please specify a resource name." );
                return;
            }

            // Check if this is the name of an existing directory
            if ( ResourceManager.ResourceDirectoryExists( proposedResourceName ) )
            {
                MessageBox.Show( "There already is a directory with that name." );
                return;
            }

            // Check for invalid chars
            if ( !ResourceManager.IsValidResourceName( proposedResourceName ) )
            {
                MessageBox.Show( proposedResourceName + " is not a valid resource name." );
                return;
            }

            // Check if the proposed extension matches the allowed extension
            IEnumerable<String> allowedExtensions =
                _resourceExplorer.ResourceExtensions.Split( new char[] { ' ', ',', ';' } )
                .Select( s => s.Trim() )
                .Select( s => ( s[0] == '.' ? s : "." + s ) );

            String extension = Path.GetExtension( proposedResourceName );

            if ( !allowedExtensions.Contains( extension ) && !allowedExtensions.Contains( ".*" ) )
            {
                MessageBox.Show( "The resource name must have one of these extensions: " +
                    _resourceExplorer.ResourceExtensions );
                return;
            }

            if ( ResourceManager.ResourceExists( proposedResourceName ) )
            {
                var result = MessageBox.Show( "A resource with the name " + proposedResourceName +
                    " already exists. Do you want to overwrite it?", "Overwrite Resource?",
                    MessageBoxButtons.OKCancel, MessageBoxIcon.Question, 
                    MessageBoxDefaultButton.Button2 );

                if ( result != DialogResult.OK )
                {
                    return;
                }
            }

            _selectedResourceName = proposedResourceName;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void _okButton_Click( object sender, EventArgs e )
        {
            _tryCloseWithOk();
        }

        private void _selectionTextBox_KeyDown( object sender, KeyEventArgs e )
        {
            if ( e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return )
            {
                e.Handled = true;
                e.SuppressKeyPress = true;
                _tryCloseWithOk();
            }
        }
    }
}
